/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.coreGenerator.base.entitygen.determination.Base.Method;

import com.inspur.edp.bef.component.detailcmpentity.determination.DeterminationComponent;
import com.inspur.edp.bef.component.detailcmpentity.determination.DeterminationMethod;
import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.cef.api.determination.ICefDeterminationContext;
import com.inspur.edp.cef.entity.changeset.IChangeDetail;
import com.inspur.edp.cef.generator.coreGenerator.base.entitygen.determination.Base.BaseDtmGenCtx;
import com.inspur.edp.lcm.metadata.api.entity.GspMetadata;
import java.util.ArrayList;
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;

public abstract class DtmExecuteGenerator extends ClassMethodGenerator {
  protected BaseDtmGenCtx parentContext;
  private DeterminationMethod method;
  private DeterminationComponent component;

  protected DtmExecuteGenerator(BaseDtmGenCtx parentContext) {
    this.parentContext = parentContext;
  }

  @Override
  protected void doInitialize() {
    super.doInitialize();
    if (parentContext.dtm != null) {
      GspMetadata metadata = parentContext.dtmsGenCtx.
              dataTypeImplGenCtx.getParentContext().getProjContext().
              getCefGenContext().getMetadataJitContext().
              getMetadata(parentContext.dtm.getComponentId());

      //GspMetadata metadata = MetadataAdapter.getMetadata(parentContext.dtm.getComponentId());
      //    if (metadata.getHeader().getType().equals("Component")) {
      //      GspComponentMetadata gspComp =
      //          (GspComponentMetadata)
      //              ((metadata.getContent() instanceof GspComponentMetadata)
      //                  ? metadata.getContent()
      //                  : null);
      //      component = gspComp != null ? (DeterminationComponent) gspComp.getContent() : null;
      //    } else {
      component = (DeterminationComponent) metadata.getContent();
      //    }
      if (component == null) {
        throw new RuntimeException("联动计算构件不存在"); // throw new GSPException(ErrorCode.BeCompNotFound,
        // Utils.FormatExceptionMsg("联动计算构件不存在:" + dtm.ComponentId));
      }
      method = component.getDeterminationMethod();
    }
  }

  // protected override BlockSyntax  buildMethodBody()
  // {
  //    var compType = MetadataAdapter.GetCustomActionType(method.Assembly,
  // method.ClassName,parentContext.dtmsGenCtx.dataTypeImplGenCtx.coreGenContext.CefGenContext.GeneratePath, parentContext.dtmsGenCtx.dataTypeImplGenCtx.coreGenContext.CefGenContext.Metadata.RelativePath);
  //    //ReflectionUtil.CheckIsAssignableFrom(typeof(AbstractDetermination), compType);
  // }

  @Override
  protected java.util.ArrayList<ModifierKeyword> getAccessModifiers() {
    ArrayList<ModifierKeyword> result = new java.util.ArrayList<ModifierKeyword>(1);
    result.add(ModifierKeyword.PUBLIC_KEYWORD);
    return result;
  }

  private static final String itemName = "execute";

  @Override
  protected String getMethodName() {
    return itemName;
  }

  protected static final String ParamContext = "context";
  protected static final String ParamChange = "change";
  // void Execute(ICefDeterminationContext context, IChangeDetail change);
  @Override
  protected java.util.ArrayList<ParameterInfo> getParameterCollection() {
    ParameterInfo tempVar = new ParameterInfo();
    tempVar.setParamName(ParamContext);
    tempVar.setParamType(new TypeInfo(ICefDeterminationContext.class));
    ParameterInfo tempVar2 = new ParameterInfo();
    tempVar2.setParamName(ParamChange);
    tempVar2.setParamType(new TypeInfo(IChangeDetail.class));
    return new java.util.ArrayList<ParameterInfo>(
        java.util.Arrays.asList(new ParameterInfo[] {tempVar, tempVar2}));
  }

  @Override
  protected TypeInfo getReturnType() {
    return new TypeInfo("void");
  }
}
